
package com.jf.cloud.search.listener;

import cn.hutool.core.util.StrUtil;
import cn.throwx.canal.gule.model.CanalBinLogResult;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.search.bo.ShopDetailBO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Objects;

/**
 * @author zz
 * @date 2021/01/21
 */
@Component
public class ShopDetailCanalListener extends BaseCanalBinlogEventThrowExceptionProcessor<ShopDetailBO> {

    private static final Logger log = LoggerFactory.getLogger(ShopDetailCanalListener.class);
    @Autowired
    private SpuFeignClient spuFeignClient;

    /**
     * 更新店铺
     * @param result
     */
    @Override
    protected void processUpdateInternal(CanalBinLogResult<ShopDetailBO> result) {
        ShopDetailBO beforeData = result.getBeforeData();
        ShopDetailBO afterData = result.getAfterData();
        // 删除店铺缓存
        RedisUtil.del(CacheNames.SHOP_DETAIL_ID_KEY + CacheNames.UNION + afterData.getShopId());
        boolean isBlank = Objects.isNull(beforeData.getShopName()) && StrUtil.isBlank(beforeData.getShopLogo()) && Objects.isNull(beforeData.getShopStatus());
        if (isBlank) {
            return;
        }
        // 更新商品信息
        ServerResponseEntity<Void> spuRes = spuFeignClient.updateSpuUpdateTimeByShopIds(Collections.singletonList(afterData.getShopId()), SysTypeEnum.MULTISHOP.value());
        if (spuRes.isFail()) {
            throw new LuckException(spuRes.getMsg());
        }
    }
}
